/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Simulation;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author epulapp
 */
public class Simulateur extends Thread {

    private int etat = _ETEINS_;
    public final static int _DEMARRE_ = 1;
    public final static int _ETEINS_ = -1;
    public final static int _PAUSE_ = 0;
    
    private int vitesse = _NORMAL_;
    public final static int _RAPIDE_ = 100;
    public final static int _LENT_ = 1500;
    public final static int _NORMAL_ = 800;
    private Simulable simul;
    
    private int mode = _ALEATOIRE_;
    public final static int _ALEATOIRE_ = 0;
    public final static int _PERSO_ = 1;
    public final static int _MODEL_ = -1;
    

    public Simulateur(Simulable sim) {
        this.simul = sim;
    }

    public synchronized void play() {
        this.setEtat(_DEMARRE_);
        this.notify();
    }

    public synchronized void stopSim() {
        this.setEtat(_ETEINS_);
    }

    public synchronized void pause() {
        this.setEtat(_PAUSE_);
    }
    
    public synchronized void changeVitesse(int vitesse)
    {
        this.setVitesse(vitesse);
    }

    public void run() {

        while (true) {
            if (this.getEtat() == _DEMARRE_) {
                try {
                    this.getSimul().Calculer();
                    Thread.sleep(this.getVitesse());
                } catch (InterruptedException ex) {
                    Logger.getLogger(Simulateur.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (this.getEtat() == _PAUSE_) {
                try {
                    synchronized (this) {
                        this.wait();
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(Simulateur.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else 
            {
                try {
                    synchronized (this) {
                        this.wait();
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(Simulateur.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }

    /**
     * @return the etat
     */
    public int getEtat() {
        return etat;
    }

    /**
     * @param etat the etat to set
     */
    public void setEtat(int etat) {
        this.etat = etat;
    }

    /**
     * @return the vitesse
     */
    public int getVitesse() {
        return vitesse;
    }

    /**
     * @param vitesse the vitesse to set
     */
    public void setVitesse(int vitesse) {
        this.vitesse = vitesse;
    }

    /**
     * @return the simul
     */
    public Simulable getSimul() {
        return simul;
    }

    /**
     * @param simul the simul to set
     */
    public void setSimul(Simulable simul) {
        this.simul = simul;
    }

    /**
     * @return the mode
     */
    public int getMode() {
        return mode;
    }

    /**
     * @param mode the mode to set
     */
    public void setMode(int mode) {
        this.mode = mode;
    }
}
